package com.can.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.can.domain.ResponseResult;
import com.can.domain.entity.ArticleTag;
import com.can.domain.vo.ArticleVO;
import com.can.domain.vo.PageVO;
import com.can.service.ArticleService;
import com.can.service.ArticleTagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 文章 控制层
 *
 * @author cancan
 * @since 2023-05-12 12:14:39
 */
@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleTagService articleTagService;

    /**
     * 新增文章
     */
    @PostMapping
    public ResponseResult article(@RequestBody ArticleVO articleVO) {
        return articleService.add(articleVO);
    }


    /**
     * 文章列表
     */
    @GetMapping("/list")
    public ResponseResult<PageVO> article(Integer pageNum, Integer pageSize, ArticleVO articleVO) {
        return ResponseResult.okResult(articleService.page(pageNum, pageSize, articleVO));
    }

    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable Long id) {
        return articleService.getArticleDetail(id);
    }

    @PutMapping
    public ResponseResult update(@RequestBody @Valid ArticleVO articleVO) {
        return articleService.update(articleVO);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        articleService.removeById(id);
        articleTagService.remove(new LambdaQueryWrapper<ArticleTag>().eq(ArticleTag::getArticleId,id));
        return ResponseResult.okResult();
    }
}

